package progappsproject1;

import java.awt.Component;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author jordan
 */
public class LoginInterface extends javax.swing.JFrame{

    /**
     * Creates new form LoginInterface
     */
    public LoginInterface() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jFrame1 = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        studentInputLabel = new javax.swing.JLabel();
        studentInput = new javax.swing.JTextField();
        logOutButton = new javax.swing.JButton();
        logInButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        jButton2.setText("jButton1");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(null);

        studentInputLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        studentInputLabel.setForeground(new java.awt.Color(255, 255, 255));
        studentInputLabel.setText("I.D. Number");
        jPanel1.add(studentInputLabel);
        studentInputLabel.setBounds(30, 20, 150, 54);

        studentInput.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        studentInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                studentInputActionPerformed(evt);
            }
        });
        jPanel1.add(studentInput);
        studentInput.setBounds(160, 20, 214, 54);

        logOutButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\lenovo-pad\\Desktop\\2\\ProgAppsProject1\\nbproject\\1374570_532788600124121_375607402_a.jpg")); // NOI18N
        logOutButton.setText("Log Out");
        logOutButton.setEnabled(false);
        logOutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logOutButtonActionPerformed(evt);
            }
        });
        jPanel1.add(logOutButton);
        logOutButton.setBounds(220, 90, 142, 55);

        logInButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\lenovo-pad\\Desktop\\2\\ProgAppsProject1\\nbproject\\1368812_532789123457402_2106998537_a.jpg")); // NOI18N
        logInButton.setText("Log In");
        logInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logInButtonActionPerformed(evt);
            }
        });
        logInButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                logInButtonKeyPressed(evt);
            }
        });
        jPanel1.add(logInButton);
        logInButton.setBounds(20, 90, 140, 55);

        jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\lenovo-pad\\Desktop\\2\\ProgAppsProject1\\nbproject\\Blue-Background-Images-HD-Wallpaper-1080x607.jpg")); // NOI18N
        jLabel2.setText("jLabel2");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, -40, 380, 220);

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, -8, 380, 150));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void studentInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_studentInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_studentInputActionPerformed

    private void logOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logOutButtonActionPerformed
        // TODO add your handling code here:
        String idNumber = studentInput.getText();
        int studId = Integer.parseInt(idNumber);
        teacherInterface.studentLogOut(studId);
    }//GEN-LAST:event_logOutButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void logInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logInButtonActionPerformed
        // TODO add your handling code here:
        studentTimeIn();
    }//GEN-LAST:event_logInButtonActionPerformed

    private void logInButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_logInButtonKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_logInButtonKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginInterface().setVisible(true);
            }
        });
    }
    
    void studentTimeIn (){

        
         try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/progapps";
            String name = "root";
            String password = "brizzalix";
            try {


        java.sql.Connection con = DriverManager.getConnection(url, name, password);
         // pull data from the database 
        java.sql.Statement stmts = null;
        String query = " select studid from studentinfo";
        stmts = con.createStatement();
        ResultSet rs = stmts.executeQuery(query);
        
        try{
            String idNumber = studentInput.getText();
        int studId = Integer.parseInt(idNumber);
        boolean isFound = false;
        rs.next();
            while(rs.next()){
                if(studId == rs.getInt("studid") ){
                    isFound = true;
                    logInButton.setEnabled(false);
                    logOutButton.setEnabled(true);
                    ProcessDataFromStudent pd = new ProcessDataFromStudent();
                    pd.updateTeacherInterface(studId);
                    break;
                }       
        }
            if( rs.next()== false){
                Component frame = null;
                JOptionPane.showMessageDialog(frame, "Incorrect ID Number!");           
                }
        }catch (NumberFormatException e){
                Component frame = null; 
                JOptionPane.showMessageDialog(frame, "Error:ID Number should consists of Number!");            
        }
        

            
            } catch (SQLException e) {
                e.printStackTrace();

            }

        } catch (ClassNotFoundException e) {
                                e.printStackTrace();
        }     
    }
    TeacherInterface ti = new TeacherInterface();
    // Variables declaration - do not modify
    private javax.swing.JButton jButton2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JButton logInButton;
    private javax.swing.JButton logOutButton;
    private javax.swing.JTextField studentInput;
    private javax.swing.JLabel studentInputLabel;
    // End of variables declaration
}